package com.ruoyi.order.service.impl;

import com.repair.api.order.request.OrderAddGrabPoolRequest;
import com.repair.api.order.request.OrderAssignRequest;
import com.repair.api.order.request.OrderQueryPageRequest;
import com.repair.api.order.request.OrderQueryRequest;
import com.repair.api.order.request.param.OrderAssignParam;
import com.repair.api.order.request.param.OrderQueryParam;
import com.repair.api.order.response.OrderAddGrabPoolResponse;
import com.repair.api.order.response.OrderAssignResponse;
import com.repair.api.order.response.OrderQueryResponse;
import com.repair.api.order.response.vo.OrderVO;
import com.repair.api.order.service.OrderFacadeService;
import com.repair.base.exception.BizErrorCode;
import com.repair.base.exception.BizException;
import com.repair.base.response.PageResponse;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

/**
 * @author chunyu
 * @since 2024/10/19,17:52
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @DubboReference
    private OrderFacadeService orderFacadeService;

    @Override
    public TableDataInfo getAllOrders(OrderQueryParam orderQueryParam) {
        log.debug("获取全部订单：{}", orderQueryParam);
        OrderQueryPageRequest<OrderQueryParam> request = new OrderQueryPageRequest<>();
        request.setCurrentPage(orderQueryParam.getCurrentPage());
        request.setPageSize(orderQueryParam.getPageSize());
        request.setData(orderQueryParam);
        PageResponse<OrderVO> response = orderFacadeService.getAllOrders(request);
        if (!response.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setRows(response.getData());
        tableDataInfo.setTotal(response.getTotal());
        return tableDataInfo;
    }

    @Override
    public OrderVO getOrderDetail(String orderNum) {
        OrderQueryRequest request = new OrderQueryRequest(orderNum);
        OrderQueryResponse<OrderVO> response = orderFacadeService.getOrderInfoByAdmin(request);
        if (!response.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
        return response.getData();
    }

    @Override
    public void assignOrder(OrderAssignParam param) {
        OrderAssignRequest<OrderAssignParam> request = new OrderAssignRequest<>();
        request.setData(param);
        OrderAssignResponse<Boolean> response = orderFacadeService.assignOrderToWorker(request);
        if (!response.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
    }

    @Override
    public void addGrabPool(String orderNum) {
        OrderAddGrabPoolRequest request = new OrderAddGrabPoolRequest();
        request.setOrderNum(orderNum);
        OrderAddGrabPoolResponse<Boolean> response = orderFacadeService.addOrderToGrabPool(request);
        if (!response.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
    }
}
